<?php
require_once 'nrg/num.class.js';
?>

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

Num.locale = {
	dec_point: '.',
	thousands_sep: ',',
	price_decimals: 2,
	stock_decimals: 4,
	bd_scale: 11
};

//------------------------------------------------------------------------------

Num.strict = function(value)
{
	value = parseFloat( Num.bdStrict(value) );
	if ( isNaN(value) ) {
		value = 0;
	}
	return value;
};

//------------------------------------------------------------------------------

Num.bdStrict = function(value)
{
	value = value.toString().replace(',', '.');
	value = value.replace(' ', '');
	return value;
};

//------------------------------------------------------------------------------

Num._formatJoinParts = function(int_part, dec_part, dec_point)
{
	if ( undefined == dec_point ) {
		dec_point = '.';
	}
	
	var retval = int_part;
	
	if ( dec_part ) {
		retval += dec_point.toString() + dec_part.toString();
	}
	
	return retval;
};

//------------------------------------------------------------------------------

Num._formatIntPartThousandsSep = function(int_part, separator_str)
{
	if ( undefined == separator_str ) {
		separator_str = Num.locale.thousands_sep;
	}
	
	var int_part_f = '';
	var sign = '';
	
	if ( '-' == int_part.substring(0, 1) ) {
		sign = '-';
		int_part = int_part.substring(1);
	}
	
	for ( var i = int_part.length; i > 0; i -= 3 ) {
		var start = i - 3;
		if ( 0 > start ) {
			start = 0;
		}
		var suffix = separator_str + int_part_f;
		if ( i == int_part.length ) {
			suffix = '';
		}
		int_part_f = int_part.substring(start, i) + suffix;
	}
	
	int_part_f = sign + int_part_f;
	
	return int_part_f;
};

//------------------------------------------------------------------------------

Num._formatDecPartMinFixed = function(dec_part, precision)
{
	if ( undefined == precision ) {
		precision = Num.locale.price_decimals;
	}
	if ( ! dec_part ) {
		dec_part = '';
	}
	
	var retval = dec_part;
	var diff = precision - dec_part.length;
	
	if ( 0 < diff ) {
		retval += '0'.repeat(diff);
	}
	
	return retval;
};

//------------------------------------------------------------------------------

Num._formatDecPartFixed = function(dec_part, precision)
{
	if ( undefined == precision ) {
		precision = Num.locale.price_decimals;
	}
	if ( ! dec_part ) {
		dec_part = '';
	}
	
	if ( dec_part.length > precision ) {
		var tmp_dec = '0.' + dec_part;
		tmp_dec = Num.bdRound(tmp_dec, precision).toString();
		if ( '0' == tmp_dec.substring(0, 1) ) {
			tmp_dec = tmp_dec.substring(1);
		}
		if ( '.' == tmp_dec.substring(0, 1) ) {
			tmp_dec = tmp_dec.substring(1);
		}
		retval = tmp_dec;
	}
	else {
		retval = Num._formatDecPartMinFixed(dec_part, precision);
	}
	
	return retval;
};

//------------------------------------------------------------------------------

Num.bdAdd = function(a, b)
{
	return new Num( new Num(a).toBd().add( new Num(b).toBd() ) );
};

//------------------------------------------------------------------------------

Num.bdSub = function(a, b)
{
	return new Num( new Num(a).toBd().subtract( new Num(b).toBd() ) );
};

//------------------------------------------------------------------------------

/**
* Returns:
* 0) if a == b
* 1) if a > b
* -1) if a < b
*/
Num.bdCmp = function(a, b)
{
	return new Num(a).toBd().compareTo( new Num(b).toBd() );
};

//------------------------------------------------------------------------------

Num.bdMul = function(a, b)
{
	return new Num( new Num(a).toBd().multiply( new Num(b).toBd() ) );
};

//------------------------------------------------------------------------------

Num.bdDiv = function(a, b)
{
	return new Num( new Num(a).toBd().divide(
		new Num(b).toBd(),
		Num.locale.bd_scale,
		BigDecimal.prototype.ROUND_HALF_UP
	) );
};

//------------------------------------------------------------------------------

Num.bdPow = function(a, mod)
{
	return new Num( new Num(a).toBd().pow( new Num(mod).toBd() ) );
};

//------------------------------------------------------------------------------

Num.bdRound = function(a, precision)
{
	if ( undefined == precision ) {
		precision = 0;
	}
	return new Num( new Num(a).toBd().round(
		precision, BigDecimal.prototype.ROUND_HALF_UP
	) );
};

//------------------------------------------------------------------------------